package com.farm.configer.service.impl;

import com.farm.configer.domain.Configer;
import com.farm.configer.domain.Configerform;
import com.farm.configer.form.ConfigerFormGenerator;
import com.farm.configer.form.FormModel;
import com.farm.core.time.TimeTool;
import com.farm.template.domain.Template;
import com.farm.web.WebUtils;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import com.farm.configer.dao.ConfigerDaoInter;
import com.farm.configer.dao.ConfigerformDaoInter;
import com.farm.configer.service.ConfigerServiceInter;
import com.farm.core.sql.query.DBRule;
import com.farm.core.sql.query.DBRuleList;
import com.farm.core.sql.query.DataQuery;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Comparator;
import java.util.List;

import javax.annotation.Resource;
import com.farm.core.auth.domain.LoginUser;

/* *
 *功能：配置集服务层实现类
 *详细：
 *
 *版本：v0.1
 *作者：FarmCode代码工程
 *日期：20150707114057
 *说明：
 */
@Service
public class ConfigerServiceImpl implements ConfigerServiceInter {
	@Resource
	private ConfigerDaoInter configerDaoImpl;
	@Resource
	private ConfigerformDaoInter configerformDaoImpl;
	private static final Logger log = Logger.getLogger(ConfigerServiceImpl.class);

	@Override
	@Transactional
	public Configer insertConfigerEntity(Configer entity, LoginUser user) {
		entity.setCuser(user.getId());
		entity.setCtime(TimeTool.getTimeDate14());
		entity.setEuser(user.getId());
		entity.setEtime(TimeTool.getTimeDate14());
		return configerDaoImpl.insertEntity(entity);
	}

	@Override
	@Transactional
	public Configer editConfigerEntity(Configer entity, LoginUser user) {
		Configer entity2 = configerDaoImpl.getEntity(entity.getId());
		entity2.setEuser(user.getId());
		entity2.setEtime(TimeTool.getTimeDate14());
		entity2.setCatalogid(entity.getCatalogid());
		entity2.setName(entity.getName());
		entity2.setModel(entity.getModel());
		entity2.setCode(entity.getCode());
		entity2.setPcontent(entity.getPcontent());
		entity2.setPstate(entity.getPstate());
		configerDaoImpl.editEntity(entity2);
		return entity2;
	}

	@Override
	@Transactional
	public void deleteConfigerEntity(String id, LoginUser user) {
		List<DBRule> rules = DBRuleList.getInstance().add(new DBRule("CONFIGERID", id, "=")).toList();
		configerformDaoImpl.deleteEntitys(rules);
		configerDaoImpl.deleteEntity(configerDaoImpl.getEntity(id));
	}

	@Override
	@Transactional
	public Configer getConfigerEntity(String id) {
		// TODO 自动生成代码,修改后请去除本注释
		if (id == null) {
			return null;
		}
		return configerDaoImpl.getEntity(id);
	}

	@Override
	@Transactional
	public DataQuery createConfigerSimpleQuery(DataQuery query) {
		DataQuery dbQuery = DataQuery.init(query, "CBTG_CONFIGER  a left join CBTG_APP_CATALOG b on b.id=a.catalogid",
				"A.ID as ID,A.CATALOGID as CATALOGID,A.NAME as NAME,A.MODEL as MODEL,A.CODE as CODE,A.PCONTENT as PCONTENT,A.PSTATE AS PSTATE,B.NAME as CATALOGNAME");
		return dbQuery;
	}

	@Override
	@Transactional
	public void updateCatalog(String tids, String catalogId, LoginUser currentUser) {
		for (String templateId : WebUtils.parseIds(tids)) {
			Configer temp = configerDaoImpl.getEntity(templateId);
			temp.setCatalogid(catalogId);
			configerDaoImpl.editEntity(temp);
		}
	}

	@Override
	@Transactional
	public List<Configerform> getForms(String configerId, String model) {
		DBRuleList rlist = DBRuleList.getInstance();
		rlist.add(new DBRule("CONFIGERID", configerId, "="));
		if (StringUtils.isNotBlank(model)) {
			rlist.add(new DBRule("MODEL", model, "="));
		}
		List<Configerform> list = configerformDaoImpl.selectEntitys(rlist.toList());
		list.sort(new Comparator<Configerform>() {
			@Override
			public int compare(Configerform o1, Configerform o2) {
				return o1.getSort() - o2.getSort();
			}
		});
		return list;
	}

	
}
